<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>groupBy</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const groupBy = (arr, handler) =>
            arr
                .map(typeof handler === 'function' ? handler : val => val[handler])
                .reduce((acc, val, i) => {
                    acc[val] = (acc[val] || []).concat(arr[i]);
                    return acc;
                }, {});
        console.log(groupBy([6.1, 4.2, 6.3], Math.floor));
        console.log(groupBy(['one', 'two', 'three'], 'length'));
    </script>
</body>

</html>